﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace BaseData.UI
{
    /// <summary>
    /// 缓存管理类
    /// </summary>
    public class Cache
    {
        private static CacheManager myCache = CacheFactory.GetCacheManager();

        /// <summary>
        /// 缓存中是否存在某个缓存
        /// </summary>
        /// <param name="key">缓存名称</param>
        /// <returns>存在true，不存在false</returns>
        public static bool Contains(string key)
        {
            return myCache.Contains(key);
        }

        /// <summary>
        /// 添加缓存（默认缓存1小时）
        /// </summary>
        /// <param name="key">缓存名称</param>
        /// <param name="value">缓存值</param>
        public static void Add(string key, object value)
        {
            //设置默认过期时间(一天，零点过期)
            DateTime expireDate = DateTime.Now.AddHours(1).Date;
            Add(key, value, expireDate);
        }

        /// <summary>
        /// 添加缓存(绝对过期策略)
        /// </summary>
        /// <param name="key">缓存名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="expireTime">过期时间</param>
        public static void Add(string key, object value, DateTime expireTime)
        {
            AbsoluteTime absoluteDate = new AbsoluteTime(expireTime);
            Cache.Add(key, value, CacheItemPriority.Normal, null, absoluteDate);
        }

        /// <summary>
        /// 添加缓存(相对过期策略)
        /// </summary>
        /// <param name="key">缓存名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="cacheTime">缓存时间</param>
        public static void Add(string key, object value, TimeSpan cacheTime)
        {
            SlidingTime slidingSpan = new SlidingTime(cacheTime);
            Cache.Add(key, value, CacheItemPriority.Normal, null, slidingSpan);
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="scavengingPriority">优先级</param>
        /// <param name="refreshAction">过期事件</param>
        /// <param name="expirations">过期策略</param>
        public static void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction
            , params ICacheItemExpiration[] expirations)
        {
            myCache.Add(key, value, scavengingPriority, refreshAction, expirations);
        }

        /// <summary>
        /// 获取缓存值
        /// </summary>
        /// <param name="key">缓存名称</param>
        /// <returns>缓存值</returns>
        public static object GetData(string key)
        {
            return myCache.GetData(key);
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">缓存名称</param>
        /// <returns>缓存值</returns>
        public static void Remove(string key)
        {
            myCache.Remove(key);
        }
    }
}
